package com.lhk.appframework.manager;

import android.content.Context;

import com.lhk.appframework.factory.FactoryAppDefault;
import com.lhk.appframework.net.NetRequestCallback;
import com.lhk.appframework.net.packet.NetRequestPacket;
import com.lhk.appframework.net.packet.NetResponsePacket;
import com.lhk.appframework.net.request.login.ImplNetRequestLoginResponse;
import com.lhk.appframework.net.request.login.NetRequestLoginCallback;
import com.lhk.appframework.net.request.login.NetRequestLoginParam;
import com.lhk.appframework.net.request.login.NetRequestLoginResponse;


public class ImplManagerNetPacketRequestDefault extends BaseManager implements ManagerNetPacketRequest {

	public static final String COMMAND_REQ_LOGIN = "";
	public static final String REQ_URL = "";
	
	public static final String KEY_DATA_LOGIN_USERID = "userId";
	public static final String KEY_DATA_LOGIN_PASSWD = "passwd";
	
	private static ImplManagerNetPacketRequestDefault instance = null;
	private ImplManagerNetPacketRequestDefault() {
		
	}
	
	public static ImplManagerNetPacketRequestDefault getInstance(){
		if(instance == null){
			synchronized (ImplManagerNetPacketRequestDefault.class) {
				if(instance == null){
					instance = new ImplManagerNetPacketRequestDefault();
				}
			}
		}
		return instance;
	}


	@Override
	public void reqLogin(Context context, NetRequestLoginParam param, final NetRequestLoginCallback callback) {
		NetRequestPacket req = NetRequestPacket.newNetRequest(COMMAND_REQ_LOGIN);
		String userId = param.getUserId();
		String passwd = param.getPasswd();
		req.putData(KEY_DATA_LOGIN_USERID, userId);
		req.putData(KEY_DATA_LOGIN_PASSWD, passwd);
		
		
		ManagerHttp mh = FactoryAppDefault.getInstance().getFactoryManager().getManagerHttp();
		
		mh.post(context, REQ_URL, req, new NetRequestCallback() {

			@Override
			public void onSuccess(NetResponsePacket response) {
				String jsonData = (String) response.getData();
				
				NetRequestLoginResponse loginRes = ImplNetRequestLoginResponse.netInstantce(jsonData);
				
				callback.onSuccess(loginRes);
			}

			@Override
			public void onFailure(NetResponsePacket response) {
				String statusCode = response.getHeader().getStatusCode();
				String msgCode =  response.getHeader().getMsgCode();
				
				NetRequestLoginResponse loginRes = ImplNetRequestLoginResponse.netInstantce(statusCode, msgCode);
				
				callback.onFailure(loginRes);
			}
		});
		
	}


	

}
